<?php
/**
 * 
 * Grafico Venta 1
 *  1) Inbound por Semana/Seguimiento
 *  2) Inbound por dias
 *  3) Ventas Vs Inbound
 * 
 * 
 * @author natanael
 *
 */
class graficop extends Controller 
{
	function __construct()
	{
		parent::Controller();
		$this->load->database();
		$this->load->model(array('mtransaccionentrada','detallerentradas','mregistro','mcompania','TransaccionHistorico'));
	
	}
	
	function index()
	{
		$this->load->view('main', array('content_page' => 'grafico3',
		                                'menutop' => 5,
		                                'var'=>$this->getSalesByProdCatXML(),
		                                'var2'=>$this->pruebaoro(),
		                                'var3'=>$this->venta()));
	}
	
	function graficoProvincia(){
		
			$this->load->view('main', array('content_page' => 'graficoProvincia',
		                                'menutop' => 5,
		                                'var'=>$this->dataProvincia(),
		                                ));
		
		
	}
	
	function encodeDataURL($strDataURL, $addNoCacheStr=false) {
            if ($addNoCacheStr==true) {
		    if (strpos(strDataURL,"?")<>0)
			  $strDataURL .= "&FCCurrTime=" . Date("H_i_s");
		     else
			   $strDataURL .= "?FCCurrTime=" . Date("H_i_s");
             }

	        return urlencode($strDataURL);
	
             }

    function datePart($mask, $dateTimeStr) {
             @list($datePt, $timePt) = explode(" ", $dateTimeStr);
            $arDatePt = explode("-", $datePt);
            $dataStr = "";
 
    if (count($arDatePt) == 1) {
        list($year, $month, $day) = $arDatePt;
        
        switch ($mask) {
        case "m": return $month;
        case "d": return $day;
        case "y": return $year;
        }
      
        return (trim($month . "/" . $day . "/" . $year));
    }
    return $dataStr;
}

function renderChart($chartSWF, $strURL, $strXML, $chartId, $chartWidth, $chartHeight, $debugMode, $registerWithJS) {

	if ($strXML=="")
        $tempData = "//Set the dataURL of the chart\n\t\tchart_$chartId.setDataURL(\"$strURL\")";
    else
        $tempData = "//Provide entire XML data using dataXML method\n\t\tchart_$chartId.setDataXML(\"$strXML\")";


    $chartIdDiv = $chartId . "Div";
    $ndebugMode = $this->boolToNum($debugMode);
    $nregisterWithJS = $this->boolToNum($registerWithJS);

$render_chart = <<<RENDERCHART

	<!-- START Script Block for Chart $chartId -->
	<div id="$chartIdDiv" align="center">
		Chart.
	</div>
	<script type="text/javascript">	
		//Instantiate the Chart	
		var chart_$chartId = new FusionCharts("$chartSWF", "$chartId", "$chartWidth", "$chartHeight", "$ndebugMode", "$nregisterWithJS");
		$tempData
		//Finally, render the chart.
		chart_$chartId.render("$chartIdDiv");
	</script>	
	<!-- END Script Block for Chart $chartId -->
RENDERCHART;

  return $render_chart;
}


            function renderChartHTML($chartSWF, $strURL, $strXML, $chartId, $chartWidth, $chartHeight, $debugMode) {
  
                 $strFlashVars = "&chartWidth=" . $chartWidth . "&chartHeight=" . $chartHeight . "&debugMode=" . $this->boolToNum($debugMode);
                 if ($strXML=="")
     
                 $strFlashVars .= "&dataURL=" . $strURL;
                 else  
                 $strFlashVars .= "&dataXML=" . $strXML;

           $HTML_chart = <<<HTMLCHART
	                     <!-- START Code Block for Chart $chartId -->
	                     <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="$chartWidth" height="$chartHeight" id="$chartId">
		                      <param name="allowScriptAccess" value="always" />
		                      <param name="movie" value="$chartSWF"/>		
		                      <param name="FlashVars" value="$strFlashVars" />
		                      <param name="quality" value="high" />
		                      <embed src="$chartSWF" FlashVars="$strFlashVars" quality="high" width="$chartWidth" height="$chartHeight" name="$chartId" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
	                    </object>
	                    <!-- END Code Block for Chart $chartId -->
HTMLCHART;

  return $HTML_chart;
}


        function boolToNum($bVal) {
                 return (($bVal==true) ? 1 : 0);
           }
	


function getSalesByProdCatXML() {
   
	$intYear = 1996;
	$addJSLinks = true;
	$forDataURL= false;
	
	$cadena = $this->ultimo($this->semana(3));
	$cadena = explode('+',$cadena);
	$cantida1 = $this->mtransaccionentrada->LoadTransacionCantidadFiltro($cadena[0],$cadena[1]);
	
	
	$ors['CategoryID'] = 22;

	      $strCat = "<categories>";

	           $strAmtDS = "<dataset seriesname='Inbound'>";
          	    $strQtyDS = "<dataset seriesName='Seguimiento Inbound' parentYAxis='S'>";
          	    
          	   

          	         for ($i = 0;$i < 9;$i++)
          	         {   
          	         	    $var = 8 - $i;
          	         	    $semana = date('W')-$var;
          	         		$cadena = $this->ultimo($this->semana($var));
	                        $cadena = explode('+',$cadena);
	                          
	                              $arrayWhereH = array( 'FechaSistema >=' => $cadena[0],
	                                                    'FechaSistema <=' => $cadena[1]);
	                              
	                              $arrayWhereR = array( 'DateSistema >=' => $cadena[0],
	                                                    'DateSistema <=' => $cadena[1]); 
	                        
	                        $cantidad = $this->detallerentradas->LoadDetalleCantidadFiltro($cadena[0],$cadena[1]);
	                        $cantidar = $this->mtransaccionentrada->loadCantByWhere($arrayWhereR);
	                        $cantidah = $this->TransaccionHistorico->loadCantByWhere($arrayWhereH);
	                        
	                        $cantida1 = $cantidar + $cantidah;
	                        
          	         	$strCat .= "<category label='".$semana."'/>";
                        $strAmtDS .= "<set value='".$cantida1."' />";
                        $strQtyDS .= "<set value='".$cantidad."'/>";
          	         }
          	        
         
	                	
                            
	         $strCat .= "</categories>";
	         
	                 
	                     
	                     
	                     
	$strAmtDS .= "</dataset>";
	$strQtyDS .= "</dataset>";

	$strXML2 = $strCat . $strAmtDS . $strQtyDS;
	$strXML  = "<chart caption='Inbound Por Semanas' palette='' animation='' formatNumberScale='0' PYAXisName='Inbound' SYAxisName='Seguimiento Inbound' numberPrefix='' labeldisplay='STAGGER' seriesNameInToolTip='0' showValues='0' plotSpacePercent='0'>";
    $strXML.=$strXML2;
    $strXML .= "<styles><definition><style type='font' name='CaptionFont' size='15' color='' /><style type='font' name='SubCaptionFont' bold='0' /></definition><application><apply toObject='caption' styles='CaptionFont' /><apply toObject='SubCaption' styles='SubCaptionFont' /></application></styles>";
	$strXML .= "</chart>";
	

	return $this->renderChart(base_url()."public/swf_charts/MSColumn3DLineDY.swf", "fdg",$strXML, "CatCum", 750,300, false, false);
	
}

function pruebaoro() {
   
	$intYear = 1996;
	$addJSLinks = true;
	$forDataURL= false;
	$ors['CategoryID'] = 22;

	      $strCat = "<categories>";

	           $strAmtDS = "<dataset seriesname='Inbound'>";
          	    $strQtyDS = "<dataset seriesName='Seguimiento Inbound' parentYAxis='S'>";
 
                       for ($i=0;$i < 15; $i++)
                       {
                       	
                       	$cant = 14 - $i;
                       	
                       	$array = $this->semana2();
                       	
                       	 $arrayWhereH = array( 'FechaSistema >=' => $array[$cant],
	                                           'FechaSistema <=' => $array[$cant]);
	                              
	                     $arrayWhereR = array( 'DateSistema >=' => $array[$cant],
	                                           'DateSistema <=' => $array[$cant]); 
	                     
                       	    $cantidar = $this->mtransaccionentrada->loadCantByWhere($arrayWhereR);
	                        $cantidah = $this->TransaccionHistorico->loadCantByWhere($arrayWhereH);
	                        
	                    $cantisa = $cantidar+$cantidah;  
                       
                       	$cantida1 = $this->detallerentradas->LoadDetalleCantidadFiltro($array[$cant],$array[$cant]);
                       	$strCat .= "<category label='".$array[$cant]."'/>";
                         $strAmtDS .= "<set value='".$cantisa."' />";
                         $strQtyDS .= "<set value='".$cantida1."'/>";
                       	
                       }        
	                	
	                	
  
             
	$strCat .= "</categories>";
	$strAmtDS .= "</dataset>";
	$strQtyDS .= "</dataset>";

	$strXML2 = $strCat . $strAmtDS . $strQtyDS;
	
	$strXML  = "<chart caption='Inbound Por Dias' palette='' animation='' formatNumberScale='0' PYAXisName='Inbound' SYAxisName='Seguimiento Inbound' numberPrefix='' labeldisplay='STAGGER' seriesNameInToolTip='0' showValues='0' plotSpacePercent='0'>";
    $strXML.=$strXML2;
    $strXML .= "<styles><definition><style type='font' name='CaptionFont' size='15' color='' /><style type='font' name='SubCaptionFont' bold='0' /></definition><application><apply toObject='caption' styles='CaptionFont' /><apply toObject='SubCaption' styles='SubCaptionFont' /></application></styles>";
	$strXML .= "</chart>";
	

	return $this->renderChart(base_url()."public/swf_charts/MSColumn3DLineDY.swf", "",$strXML, "CatCum2", 750,300, false, false);
	
}


function venta() {
   
	$intYear = 1996;
	$addJSLinks = true;
	$forDataURL= false;
	$cantisa = $this->mtransaccionentrada->LoadTransacionCantidadFiltro('2009-09-10','2009-09-15');
	$ors['CategoryID'] = 20;

	      $strCat = "<categories>";

	           $strAmtDS = "<dataset seriesname='Ventas'>";
          	    $strQtyDS = "<dataset seriesName='Inbound' parentYAxis='B'>";
 
                       for ($i=0;$i < 15; $i++)
                       {
                       	
                       	$cant = 14 - $i;
                       	
                       	 $array = $this->semana2();
                       	
                       	 $arrayWhereH = array( 'FechaSistema >=' => $array[$cant],
	                                           'FechaSistema <=' => $array[$cant],
                       	                       'IdMotivoSalida' => 6);
	                              
	                     $arrayWhereR = array( 'DateSistema >=' => $array[$cant],
	                                           'DateSistema <=' => $array[$cant]); 
	                     
	                  
	                     
                       	    $cantisa = $this->mtransaccionentrada->loadCantByWhere($arrayWhereR);
	                        $cantida1 = $this->TransaccionHistorico->loadCantByWhere($arrayWhereH);
                       	
                       	 $strCat .= "<category label='".$array[$cant]."'/>";
                         $strAmtDS .= "<set value='".$cantida1."' />";
                         $strQtyDS .= "<set value='".$cantisa."'/>";
                       	
                       }        
	                	
	                	
  
             
	$strCat .= "</categories>";
	$strAmtDS .= "</dataset>";
	$strQtyDS .= "</dataset>";

	$strXML2 = $strCat . $strAmtDS . $strQtyDS;
	
	$strXML  = "<chart caption='Ventas Por Dias' palette='' animation='' formatNumberScale='0' PYAXisName='Ventas' SYAxisName='Inbound' numberPrefix='' labeldisplay='STAGGER' seriesNameInToolTip='0' showValues='0' plotSpacePercent='0'>";
    $strXML.=$strXML2;
    $strXML .= "<styles><definition><style type='font' name='CaptionFont' size='15' color='' /><style type='font' name='SubCaptionFont' bold='0' /></definition><application><apply toObject='caption' styles='CaptionFont' /><apply toObject='SubCaption' styles='SubCaptionFont' /></application></styles>";
	$strXML .= "</chart>";
	

	return $this->renderChart(base_url()."public/swf_charts/MSColumn3DLineDY.swf", "",$strXML, "CatCum3", 750,300, false, false);
	
}

function dataProvincia()
{
	 $dataArray[0][1]="01"; //Azua 3
      $dataArray[0][2]= $this->cantidadPrivincia(3); //Population 
      
      $dataArray[1][1]="02"; //Bahoruco 
      $dataArray[1][2]= $this->cantidadPrivincia(4); //Population
      
      $dataArray[2][1]="03"; //Barahona
      $dataArray[2][2]= $this->cantidadPrivincia(5); //Population
      
      
      $dataArray[3][1]="04"; //Dajabon
      $dataArray[3][2]= $this->cantidadPrivincia(6); //Populatio
      
      $dataArray[4][1]="05"; //No se save
      $dataArray[4][2]="4"; //Population
      
      $dataArray[5][1]="06"; //Duarte
      $dataArray[5][2]= $this->cantidadPrivincia(7); //Populatio 
      
      
      $dataArray[6][1]="07"; //nose save
      $dataArray[6][2]= "1"; //Population
      
      
      $dataArray[7][1]="08"; //Espaillat
      $dataArray[7][2]=$this->cantidadPrivincia(10); //Population
      
      $dataArray[8][1]= "09"; //Independencia
      $dataArray[8][2]=$this->cantidadPrivincia(13); //Population
      
      $dataArray[9][1]="10"; //La Altagracia
      $dataArray[9][2]=$this->cantidadPrivincia(14); //Population 
      
      $dataArray[10][1]="11"; //Elias Pina
      $dataArray[10][2]=$this->cantidadPrivincia(9); //Population 
      
      $dataArray[11][1]="12"; //La Romana
      $dataArray[11][2]=$this->cantidadPrivincia(15); //Population
      
      $dataArray[12][1]="13"; //No Save
      $dataArray[12][2]="12"; //Populatio
      
      $dataArray[13][1]="14"; //Maria  Trinidad Sanchez
      $dataArray[13][2]=$this->cantidadPrivincia(17); //Population
      
      $dataArray[14][1]="15"; //monte Cristi
      $dataArray[14][2]=$this->cantidadPrivincia(19); //Populatio
      
      $dataArray[15][1]="16"; //Pedernales
      $dataArray[15][2]=$this->cantidadPrivincia(22); //Population
      
      $dataArray[16][1]="17"; //Nose Save
      $dataArray[16][2]="16"; //Population
      
      $dataArray[17][1]="18"; //Puerto Plata
      $dataArray[17][2]=$this->cantidadPrivincia(23); //Population
      
      $dataArray[18][1]="19"; //Hermanas Mirabal
      $dataArray[18][2]=$this->cantidadPrivincia(12); //Population
      
      $dataArray[19][1]="20"; //Samana
      $dataArray[19][2]=$this->cantidadPrivincia(24); //Population
      
      $dataArray[20][1]="21"; //Sanchez Ramirez
      $dataArray[20][2]=$this->cantidadPrivincia(29); //Population
      
      $dataArray[21][1]="22"; //Nose Save
      $dataArray[21][2]="21"; //Population
      
      $dataArray[22][1]="23"; //San Juan
      $dataArray[22][2]=$this->cantidadPrivincia(27); //Populatio 
      
      $dataArray[23][1]="24"; //San Pedro De macori
      $dataArray[23][2]=$this->cantidadPrivincia(28); //Population 
      
      $dataArray[24][1]="25"; //Santiago
      $dataArray[24][2]=$this->cantidadPrivincia(30); //Populatio
      
      $dataArray[25][1]="26"; //Santiago Rodriguez
      $dataArray[25][2]=$this->cantidadPrivincia(29); //Population
      
      $dataArray[26][1]="27"; //Valverde
      $dataArray[26][2]=$this->cantidadPrivincia(33); //Population
      
      $dataArray[27][1]="28"; //El Seybo
      $dataArray[27][2]=$this->cantidadPrivincia(8); //Population
      
      $dataArray[28][1]="29"; //Hato Mayor
      $dataArray[28][2]=$this->cantidadPrivincia(11); //Population
      
      $dataArray[29][1]="30"; //La Vega
      $dataArray[29][2]=$this->cantidadPrivincia(16); //Population
      
      $dataArray[30][1]="31"; //Monseno Noel
      $dataArray[30][2]=$this->cantidadPrivincia(18); //Population
      
      $dataArray[31][1]="32"; //Monte Plata
      $dataArray[31][2]=$this->cantidadPrivincia(20); //Population
      
      $dataArray[32][1]="33"; //San Critobal
      $dataArray[32][2]=$this->cantidadPrivincia(25); //Population
      
      $dataArray[33][1]="34"; //Distrito Nacional
      $dataArray[33][2]=$this->cantidadPrivincia(1); //Population
      
      $dataArray[34][1]="35"; //Peravia
      $dataArray[34][2]=$this->cantidadPrivincia(22); //Population
      
      $dataArray[35][1]="36"; //San Jose de Ocoa
      $dataArray[35][2]=$this->cantidadPrivincia(26); //Population
      
      $dataArray[36][1]="37"; //Santo Domingo
      $dataArray[36][2]=$this->cantidadPrivincia(32); //Population  
	$strXML="";
	
	# Opening MAP element
	$strXML = "<map showLabels='1' includeNameInLabels='1' borderColor='FFFFFF' fillAlpha='80' showBevel='0' legendPosition='Bottom' >";
    
	# Setting Color ranges : 4 color ranges for population ranges
	$strXML .= "<colorRange>";
	$strXML .= "<color minValue='0' maxValue='1' displayValue='Venta : 0 C24H' color='cccccc' />";
	$strXML .= "<color minValue='1' maxValue='10' displayValue='Venta : 1 - 10 C24H' color='ff5544' />";
	$strXML .= "<color minValue='10' maxValue='30' displayValue='Venta :10 - 30 C24H' color='ffff00' />";
	$strXML .= "<color minValue='30' maxValue='75' displayValue='Venta :30 - 75 C24H' color='009900' />";
	$strXML .= "<color minValue='75' maxValue='2000' displayValue='Venta : 75 - .. C24H' color='0000ee' />";
	$strXML .= "</colorRange><data>";
	
	# Opening data element that will store map data
	# Using Data from array for each entity 
	for($i=0;$i< 37;$i++){
		$strXML .= "<entity id='" . $dataArray[$i][1] . "' value='" . $dataArray[$i][2] . "' />";
	}
    # closing  data element	
	$strXML .= "</data>";
	
	# closing map element
	$strXML  .= "</map>";
	return $this->renderChart(base_url()."public/swf_charts/FCMap_DominicanRepublic.swf", "",$strXML, "UsaMaps", 750,360, 0, 0);

	
}

//funciones de los dias

 function semana($di)
 {
 	        
 	          for ($i = 0;$i < 120; $i++ )
              {
	            $dia[] = date("Y-m-d-W",mktime(0,0,0,date('m'),date('d')-$i,date('Y')) );
              }
              
              
             foreach ($dia as $d)
             {
  	            $dia = explode('-',$d);
  	            $semana[intval($dia[3])][] = $dia[0].'-'.$dia[1].'-'.$dia[2];
              }
              
                $_dia = date('W')-$di;
                
                if ($_dia <= 0) {
                	$_dia = 51 + $_dia;
                }

                   $semana = $semana[$_dia];
            
  	     return $semana;
  	
 } 
 
function ultimo($array)
{
	$dia = $array[count($array)-1].'+'.$array[0];
	return $dia;
}

function cantidadPrivincia($id){
	
	$cantidad = $this->mcompania->loadCantidadWhere(array('IdProvincia'=>$id));
	return $cantidad;
}

function semana2()
 {
 	
 	          for ($i = 0;$i < 80; $i++ )
              {
	            $dia[] = date( "Y-m-d",mktime(0,0,0,date('m'),date('d')-$i,date('Y')) );
              }


            
  	     return $dia;
  	
 } 

}
?>